package com.wf.atm.pojo;

import lombok.Data;
import com.wf.atm.exception.ATMException;
import com.wf.atm.exception.BalanceNotEnoughException;

import java.io.Serializable;

/**
 * @author mpk
 * @since 2024/6/4 14:52:52
 */

@Data

public class LoanCreditAccount extends CreditAccount implements Loanable, Serializable {

    private static final long serialVersionUID = -6257399716699008447L;
    private long loanAmount;

    public LoanCreditAccount(Long id, String password, String name, String personId,String address, String email) {
        super(id, password, name, personId,address, email);

    }

    @Override
    public Account requestLoan(double money) throws BalanceNotEnoughException, ATMException {

        if(this.getBalance()<0)
        {
            throw new ATMException("透支未还清！！不允许贷款！！");
        }

        if (money < 0) {
            throw new BalanceNotEnoughException("贷款金额不能为负数！");
        }

        this.loanAmount += (long) money;
        this.deposit(money);
        return this;
    }

    @Override
    public Account payLoan(double money) throws ATMException {

        if (this.loanAmount < money) {

            throw new ATMException("还款金额过多！");
        }
        this.loanAmount -= (long) money;
        try {
            this.withdraw(money);
        } catch (BalanceNotEnoughException e) {
            System.out.println(e.getMessage());
        }
        return this;
    }

    @Override
    public String toString() {
        return "LoanCreditAccount{" +
                "loanAmount=" + loanAmount +
                "} " + super.toString();
    }
}
